package game.module.sss.logic;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.entity.PlayingRole;
import game.module.sss.SssConstants;
import game.module.sss.bean.SssRound;
import game.session.GlobalTimer;
import game.session.SessionManager;
import io.netty.util.Timeout;
import io.netty.util.TimerTask;
import lion.netty4.message.GamePlayer;
import lion.netty4.message.INetty4EventHandler;
import lua.LuaMessageSss.PushSssRoomDissmiss;

public class SssDismissRoomManager implements INetty4EventHandler{

	public static final Logger logger = LoggerFactory.getLogger(SssDismissRoomManager.class);
	
	private GlobalTimer globalTimer = GlobalTimer.getInstance();
	
	private Map<String ,MapValueHolder> roomTimeoutMap =new ConcurrentHashMap<String, MapValueHolder>();
	
	public static final int SESSION_TIMEOUT_DELAY = SssConstants.AUTODISSROOMTIME;// 150秒
	
	static class SingletonHolder {
		static SssDismissRoomManager instance = new SssDismissRoomManager();
	}

	public static SssDismissRoomManager getInstance() {
		return SingletonHolder.instance;
	}
	//取消执行
	public void cancleDissMiss(final String roomid) {
		if (!roomTimeoutMap.containsKey(roomid)) {
			return ;
		}
		MapValueHolder mapValueHolder =roomTimeoutMap.get(roomid);
		Timeout curTimeout = mapValueHolder.getWheelTimeout();
		curTimeout.cancel();
		roomTimeoutMap.remove(roomid);
	}
	//提前执行
	public void advanceCancle(Integer playerId) {
		final SssRound sssround = SssDataManager.getInstance().getNnRoom(playerId);
		if(sssround == null) {
			return; 
		}
		cancleDissMiss(sssround.getRoomUuid());
		List<Integer> playerList =  sssround.getPlayerIds();
		PushSssRoomDissmiss pushMsg = new PushSssRoomDissmiss();
		pushMsg.index =(byte) playerList.indexOf(playerId);
		pushMsg.state = 1;
		List<Integer> playerIds = sssround.getPlayerIds();
		for (Integer aPlayerId : playerIds) {
			PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
		//	SssDataManager.getInstance().removePlayer(aPlayerId);
			pr.writeAndFlush(pushMsg.build(pr.alloc()));
		}
		SssLogic.getInstance().allRoundOver(sssround);
	}
	
	
	public void createRemoveSssRound(final Integer playerId) {
		final SssRound sssround = SssDataManager.getInstance().getNnRoom(playerId);
		if(sssround == null) {
			return;//数据有误
		}
		if(roomTimeoutMap.containsKey(sssround.getRoomUuid()))
			return; //任务存在
		Timeout curTimeout = globalTimer.newTimeout(new TimerTask() {
			
			@Override
			public void run(Timeout timeout) throws Exception {
				// TODO Auto-generated method stub
				logger.info("dissmiss sssroom by vote playerid={} ",playerId);
				MapValueHolder oldValueHolder = roomTimeoutMap.get(sssround.getRoomUuid());
				SssRound ssround = oldValueHolder.getSssround();
				PushSssRoomDissmiss pushMsg = new PushSssRoomDissmiss();
				List<Integer> playerIds = ssround.getPlayerIds();
				pushMsg.index =(byte) playerIds.indexOf(playerId);
				pushMsg.state = 1;
				if(ssround!=null) {
					for (Integer aPlayerId : playerIds) {
						PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
						if (pr != null && pr.isChannelActive()) {
							pr.writeAndFlush(pushMsg.build(pr.alloc()));
							// 推送关于出牌的信息
						}
						SssDataManager.getInstance().removePlayer(aPlayerId);
					}
				}
				roomTimeoutMap.remove(sssround.getRoomUuid());
			}
		}, SESSION_TIMEOUT_DELAY);
		sssround.setCreateVotedissTime(System.currentTimeMillis());
		List<Integer> playerList =  sssround.getPlayerIds();
	    sssround.setVoteDissPlayer((byte)playerList.indexOf(playerId));
		MapValueHolder mapValueHolder = new MapValueHolder(curTimeout,sssround);
		roomTimeoutMap.put(sssround.getRoomUuid(), mapValueHolder);
	}
	
	@Override
	public void channelInactive(GamePlayer gamePlayer) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void readTimeout(GamePlayer gamePlayer) {
		// TODO Auto-generated method stub
		logger.info("read timeout,close socket!,addr={}", gamePlayer.getAddress());
		gamePlayer.close();
	}

}
class MapValueHolder {
	private Timeout wheelTimeout;
	private SssRound Sssround;

	public MapValueHolder(Timeout wheelTimeout, SssRound sssround) {
		super();
		this.setWheelTimeout(wheelTimeout);
		this.setSssround(sssround);
	}

	public Timeout getWheelTimeout() {
		return wheelTimeout;
	}

	public void setWheelTimeout(Timeout wheelTimeout) {
		this.wheelTimeout = wheelTimeout;
	}

	public SssRound getSssround() {
		return Sssround;
	}

	public void setSssround(SssRound sssround) {
		Sssround = sssround;
	}

	

	
}
